﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Linq;

public partial class messages : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            gvMessages.DataBind();
            gvMessages.SelectedIndex = 0;
            dvMessages.DataBind();
            if (Request.QueryString.Count == 0)
            {
                tbSearch.Visible = true;
                btSearch.Visible = true;
            }
            
        }
    }

    protected void dvMessages_Load(object sender, EventArgs e)
    {/*
        String data;

        foreach (DetailsViewRow r in dvMessages.Rows)
        {
            if (r.Cells.Count > 1)
            {
                data = r.Cells[1].Text;
            }
            else
            {
                data = r.Cells[0].Text;
            }

            data = data.Replace("&nbsp;", "").Trim();
            if (data == null || data == "")
            {
                r.Visible = false;
            }
        }*/
    }

    protected void gvMessages_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        dvMessages.PageIndex = e.NewSelectedIndex;
        dvMessages.DataBind();
    }

    protected void dataBinding(object sender, EventArgs e)
    {
        AnsweringServiceDataContext db = new AnsweringServiceDataContext();
        if (Request.QueryString.Count > 0 && Request.QueryString["c"] != "")
        {
            if (cbActiveOnly.Checked)
            {
                var msg = from m in db.Messages
                          where m.Call.Event.ClientID == Convert.ToInt32(Request.QueryString["c"]) && m.Active == true
                          select m;
                gvMessages.DataSource = msg;
                dvMessages.DataSource = msg;
            }
            else
            {
                var msg = from m in db.Messages
                          where m.Call.Event.ClientID == Convert.ToInt32(Request.QueryString["c"])
                          select m;
                gvMessages.DataSource = msg;
                dvMessages.DataSource = msg;
            }
        }
        else if (tbSearch.Text != "")
        {
            if (cbActiveOnly.Checked)
            {
                var msg = from m in db.Messages
                          where (m.Call.Event.Client.Name.Contains(tbSearch.Text) || m.Call.Event.Client.Number.Contains(tbSearch.Text)) && m.Active == true
                          select m;
                gvMessages.DataSource = msg;
                dvMessages.DataSource = msg;
            }
            else
            {
                var msg = from m in db.Messages
                          where m.Call.Event.Client.Name.Contains(tbSearch.Text) || m.Call.Event.Client.Number.Contains(tbSearch.Text)
                          select m;
                gvMessages.DataSource = msg;
                dvMessages.DataSource = msg;
            }
        }
        else
        {
            if (cbActiveOnly.Checked)
            {
                var msg = from m in db.Messages
                          where m.Active == true
                          
                          select m;
                gvMessages.DataSource = msg;
                dvMessages.DataSource = msg;
            }
            else
            {
                var msg = from m in db.Messages
                          select m;
                gvMessages.DataSource = msg;
                dvMessages.DataSource = msg;
            }
        }
    }

    protected void btSearch_Click(object sender, EventArgs e)
    {
        gvMessages.DataBind();
        dvMessages.DataBind();
    }

    protected void cbActiveOnly_CheckedChanged(object sender, EventArgs e)
    {
        gvMessages.DataBind();
        dvMessages.DataBind();
    }

    protected void dvMessages_DataBound(object sender, EventArgs e)
    {
        String data;

        List<int> labelRows = new List<int> {7,9,11,13,15,17,19,21,23,25,27,29};

        int i = 0;
        int firstCustomLabelIndex = 7;
        string label = "";
        foreach (DetailsViewRow r in dvMessages.Rows)
        {
            i++;
            if (i >= firstCustomLabelIndex)
                if (labelRows.Contains(i) && r.Cells[1].Text.Replace("&nbsp;","").Trim() != "")
                {
                    label = r.Cells[1].Text;
                    r.Cells[1].Text = "";
                }
                else if (label != "")
                {
                    r.Cells[0].Text = label;
                    label = "";
                }
        }
        foreach (DetailsViewRow r in dvMessages.Rows)
        {
            if (r.Cells.Count > 1)
            {
                data = r.Cells[1].Text;
            }
            else
            {
                data = r.Cells[0].Text;
            }

            data = data.Replace("&nbsp;", "").Trim();
            if (data == null || data == "")
            {
                r.Visible = false;
            }
        }


        /*
        int messageID = Convert.ToInt32(dvMessages.DataKey.Value);
        AnsweringServiceDataContext db = new AnsweringServiceDataContext();
        var getCustomFields = from m in db.Messages
                           where m.MessageID == messageID
                      select m.Call.Event.Client.CustomFields;

        foreach (var customFields in getCustomFields)
        {
            foreach (CustomField field in customFields)
            {
                BoundField f = new BoundField();
                f.HeaderText = field.Name;
                f.DataField = field.FieldColumn.FieldName;
                dvMessages.Fields.Add(f);
            }
        }
        */ 
    }

    protected void gvMessages_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton button = (LinkButton)e.Row.FindControl("btChangeActive");
            button.CommandArgument = e.Row.RowIndex.ToString();
        }
    }

    protected void gvMessages_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "change")
        {
            int msgID = (int)gvMessages.DataKeys[Convert.ToInt32(e.CommandArgument)].Value;
            AnsweringServiceDataContext db = new AnsweringServiceDataContext();
            Message message = db.Messages.Single(m => m.MessageID == msgID);
            if (message.Active)
                message.Active = false;
            else
                message.Active = true;
            db.SubmitChanges();
            gvMessages.DataBind();
            dvMessages.DataBind();
        }
    }
     
}
